package com.wuyou.mid;

import java.util.Arrays;

/**
 * @Description 670. 最大交换
 * @Author 无忧
 * @Date 2022/9/14 21:31
 */
public class MaximumSwap {


    public int solution_1(int num) {
        char[] c = String.valueOf(num).toCharArray();

        char[] c1 = String.valueOf(num).toCharArray();
        char[] c2 = new char[c.length];
        Arrays.sort(c);
        int index = 0;
        for (int i = c.length - 1; i >= 0; i--) {
            c2[index] = c[i];
            index++;
        }
        int a = 0;
        int b = 0;
        for (int i = 0; i < c.length; i++) {
            if (c2[i] != c1[i]) {
                a = c1[i];
                b = c2[i];
                break;
            }
        }
        int aIndex = 0;
        int bIndex = 0;
        for (int i = 0; i < c1.length; i++) {
            if (c1[i] == a) {
                aIndex = i;
                break;
            }
        }
        for (int i = c1.length - 1; i >= 0; i--) {
            if (c1[i] == b) {
                bIndex = i;
                break;
            }
        }
        char temp = c1[aIndex];
        c1[aIndex] = c1[bIndex];
        c1[bIndex] = temp;
        return Integer.valueOf(new String(c1));
    }
}
